/*
 * Copyright (C) 2000-2008 Jens Nixdorf and various contributors
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to
 * deal in the Software without restriction, including without limitation the
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
 * sell copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies of the Software, its documentation and marketing & publicity
 * materials, and acknowledgment shall be given in the documentation, materials
 * and software packages that this Software was used.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 * THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
package org.mopore.jat.gui;

import java.awt.Window;
import java.util.HashMap;
import java.util.Map;

import javax.swing.JFrame;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.mopore.jat.plugin.IWindowController;

/**
 * @author Jens Nixdorf
 *
 */
public class WindowController implements IWindowController {
	
	
	private final Map<String, Window> shownMap;
	
	private boolean showingExclusive;
	
	private final Log log = LogFactory.getLog(getClass().getName());
	
	private WindowControllerClosingAdapter closingAdapter;
	
	
	public WindowController() {
		shownMap = new HashMap<String, Window>();
		closingAdapter = new WindowControllerClosingAdapter( this );
	}
	
	
	public  boolean askToShow( JFrame askingWindow, boolean askingExclusive ) {
		
		final String windowKey = askingWindow.getClass().getName();
		log.debug( "window asking to be shown: " + windowKey );
		boolean denied = ! assertNotDenied(askingWindow, askingExclusive, windowKey );
		
		if ( denied ) {
			boolean notSameInstance = ! shownMap.containsValue( askingWindow );
			if ( notSameInstance ) {
				askingWindow.dispose();
			}
			log.debug( "showing window was denied" );
			return false;
		}
		
		shownMap.put( windowKey, askingWindow );
		showingExclusive = askingExclusive;
		askingWindow.setVisible( true );
		askingWindow.requestFocus();
		
		closingAdapter.attach( askingWindow,  windowKey );
		
		return true;
	}




	private boolean assertNotDenied(Window askingWindow, boolean askingExclusive, String askingWindowKey ) {
		
		if ( showingExclusive ) {
			
			Window showingExlusiveWindow = shownMap.values().iterator().next();
			showingExlusiveWindow.requestFocus();
			return false;
		}
		
		if ( askingExclusive ) {
			boolean othersShowing = ! shownMap.isEmpty();
			if ( othersShowing ) {
				return false;
			}
		}
		
		boolean alreadyShowing = shownMap.containsKey( askingWindowKey );
		if ( alreadyShowing ) {
			Window showingWindow = shownMap.get( askingWindowKey );
			showingWindow.requestFocus();
			return false;
		}
		
		return true;
	}
	
	
	void informAboutClosing( String windowKey ) {
		showingExclusive = false;
		shownMap.remove( windowKey );
		log.debug( "window informed about closing: " + windowKey );
	}

}
